package boun.center;

import java.io.IOException;
import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class BusScheduleFetcher {
//    private static final String SOURCE_URL = "http://www.boun.edu.tr/en-US/Content/About_BU/Corporate_Communication/News.aspx";
//  public static void main(String args[]) {
//      try {
//          BusSchedule b = fetchBusSchedule("http://www.iett.gov.tr/saat/orer.php?hid=hat&hatcode=59R%3ARUMEL%DD+H%DDSAR%DCST%DC+-+%DE%DD%DEL%DD&BtnSaatler2.x=38&BtnSaatler2.y=11&BtnSaatler2=BtnSaatler.x%3D43%26BtnSaatler.y%3D16");
//          System.out.println("asd");
//      } catch (IOException e) {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
//      } catch (ParseException e) {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
//      }
//  }
    public static BusSchedule fetchBusSchedule(String url) throws IOException, ParseException {
        //iso-8859-9
        String webContent = WebDownloader.fetchWebContent(url);
        //System.out.println(webContent);
        return parseBusSchedule(webContent);
    }
    
    private static BusSchedule parseBusSchedule(String content) throws IOException, ParseException {
        BusSchedule busSchedule = null;
        Pattern metadataPattern = Pattern.compile("<center><span class=haberyazisi ><b>(.*?)<br>(.*?)</b>.*?(\\d+)");
        Pattern rowPattern = Pattern.compile(
                "<tr.*?" +
                "<td colspan=1><center><font color=.*?><b>(.*?)</font></center></b></td>" +
                "<td colspan=1><center><font color=.*?><b>(.*?)</font></center></b></td>" +
                "<td colspan=1><center><font color=.*?><b>(.*?)</font></center></b></td>" +
                "<td colspan=1><center><font color=.*?><b>(.*?)</font></center></b></td>" +
                "<td colspan=1><center><font color=.*?><b>(.*?)</font></center></b></td>" +
                "<td colspan=1><center><font color=.*?><b>(.*?)</font></center></b></td>" +
                ".*?</tr>");
        Pattern timePattern = Pattern.compile(".*?(\\d\\d:\\d\\d).*?");
        Matcher matcher = metadataPattern.matcher(content);
        if (matcher.find()) {
            String lineCode = matcher.group(1).trim();
            String[] locations = matcher.group(2).split("-");
            String roundTripTime = matcher.group(3).trim();
            busSchedule = new BusSchedule(lineCode, locations[0].trim(), locations[1].trim(), roundTripTime);
            matcher = rowPattern.matcher(content);
            while (matcher.find()) {
                if (matcher.groupCount() == 6) {
                    for (int i = 1; i <= 6; ++i) {
                        BusSchedule.DayType dayType =
                                i % 3 == 1 ? BusSchedule.DayType.WEEK_DAY :
                                i % 3 == 2 ? BusSchedule.DayType.WEEK_END : BusSchedule.DayType.HOLIDAY;
                        BusSchedule.Direction direction = i <= 3 ? BusSchedule.Direction.FROM_LOCATION1_TO_LOCATION2 :
                                                                   BusSchedule.Direction.FROM_LOCATION2_TO_LOCATION1;
                        
                        String timePortion = matcher.group(i).trim();
                        Matcher timeMatcher = timePattern.matcher(timePortion);
                        if (timeMatcher.matches()) {
                            String time = timeMatcher.group(1).trim();
                            busSchedule.insertDailyTime(direction, dayType, time);
                        }
                    }
                }
            }
        }
        return busSchedule;
    }
}
